package com.quanyan.place.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.entity.apireq.ReqPlaceOrderVo;
import com.quanyan.place.entity.apireq.ReqSurveyPlace;
import com.quanyan.place.entity.apireq.ReqSurveyPlaceBaseInfo;
import com.quanyan.place.entity.apireq.ReqSurveyPlaceExtInfo;
import com.quanyan.place.service.ApiPlaceSurveyService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by youxiaojia on 2017/2/21.
 */
@RestController
@RequestMapping("/api/place/survey")
public class ApiPlaceSurveyController {
    private static final Logger logger = LoggerFactory.getLogger(ApiPlaceSurveyController.class);
    @Autowired
    ApiPlaceSurveyService apiPlaceSurveyService;

    @RequestMapping(value = "/city/{cityId}/districts",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getDistrictList(@PathVariable("cityId") @Valid Integer cityId){
        return apiPlaceSurveyService.getDistrictListByCityId(cityId);
    }

    @RequestMapping(value = "/city/{cityId}",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getSurveyPlaceList(@PathVariable("cityId") @Valid Integer cityId, @RequestBody @Valid ReqSurveyPlace reqSurveyPlace, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        reqSurveyPlace.setCityId(cityId);
        return apiPlaceSurveyService.getSurveyPlaceListByCondition(reqSurveyPlace);
    }

    @RequestMapping(value = "/saveSurveyPlaceBaseInfo",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> saveSurveyPlaceBaseInfo(@RequestBody @Valid ReqSurveyPlaceBaseInfo reqSurveyPlaceBaseInfo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return apiPlaceSurveyService.submitSurveyPlaceBaseInfo(reqSurveyPlaceBaseInfo);
    }

    @RequestMapping(value = "/saveSurveyPlaceExtInfo",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> saveSurveyPlaceExtInfo(@RequestBody @Valid ReqSurveyPlaceExtInfo reqSurveyPlaceExtInfo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return apiPlaceSurveyService.submitSurveyPlaceExtInfo(reqSurveyPlaceExtInfo);
    }

    @RequestMapping(value = "/getSurveyPlaceBaseInfo",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getSurveyPlaceBaseInfo(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        return apiPlaceSurveyService.getSurveyPlaceBaseInfo(placeId);
    }

    @RequestMapping(value = "/getSurveyPlaceExtInfo",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getSurveyPlaceExtInfo(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        return apiPlaceSurveyService.getSurveyPlaceExtInfo(placeId);
    }
}
